<?php
/*
 * Class that operate on table 'categorymaster'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2009-09-12 17:20
 */
class CategorymasterMySqlDAO implements CategorymasterDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return CategorymasterMySql 
	 */
	public function load($id){
		$sql = 'SELECT * FROM categorymaster WHERE CategoryMasterId = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($id);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAll(){
		$sql = 'SELECT * FROM categorymaster';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllOrderBy($orderColumn){
		$sql = 'SELECT * FROM categorymaster ORDER BY '.$orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param categorymaster primary key
 	 */
	public function delete($CategoryMasterId){
		$sql = 'DELETE FROM categorymaster WHERE CategoryMasterId = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($CategoryMasterId);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param CategorymasterMySql categorymaster
 	 */
	public function insert($categorymaster){
		$sql = 'INSERT INTO categorymaster (CategoryName, CategoryDescription, Url, MarkForDelete, Position) VALUES (?, ?, ?, ?, ?)';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($categorymaster->categoryName);
		$sqlQuery->set($categorymaster->categoryDescription);
		$sqlQuery->set($categorymaster->url);
		$sqlQuery->setNumber($categorymaster->markForDelete);
		$sqlQuery->setNumber($categorymaster->position);

		$id = $this->executeInsert($sqlQuery);	
		$categorymaster->categoryMasterId = $id;
		return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param CategorymasterMySql categorymaster
 	 */
	public function update($categorymaster){
		$sql = 'UPDATE categorymaster SET CategoryName = ?, CategoryDescription = ?, Url = ?, MarkForDelete = ?, Position = ? WHERE CategoryMasterId = ?';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($categorymaster->categoryName);
		$sqlQuery->set($categorymaster->categoryDescription);
		$sqlQuery->set($categorymaster->url);
		$sqlQuery->setNumber($categorymaster->markForDelete);
		$sqlQuery->setNumber($categorymaster->position);

		$sqlQuery->setNumber($categorymaster->categoryMasterId);
		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'TRUNCATE TABLE categorymaster';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByCategoryName($value){
		$sql = 'SELECT * FROM categorymaster WHERE CategoryName = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByCategoryDescription($value){
		$sql = 'SELECT * FROM categorymaster WHERE CategoryDescription = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByUrl($value){
		$sql = 'SELECT * FROM categorymaster WHERE Url = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByMarkForDelete($value){
		$sql = 'SELECT * FROM categorymaster WHERE MarkForDelete = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByPosition($value){
		$sql = 'SELECT * FROM categorymaster WHERE Position = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}


	public function queryByExistingPosition($value,$value1){
		$sql = 'SELECT * FROM categorymaster WHERE Position = ? AND CategoryMasterId NOT in ( ? )';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		$sqlQuery->setNumber($value1);
		return $this->getList($sqlQuery);
	}
	public function deleteByCategoryName($value){
		$sql = 'DELETE FROM categorymaster WHERE CategoryName = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByCategoryDescription($value){
		$sql = 'DELETE FROM categorymaster WHERE CategoryDescription = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByUrl($value){
		$sql = 'DELETE FROM categorymaster WHERE Url = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByMarkForDelete($value){
		$sql = 'DELETE FROM categorymaster WHERE MarkForDelete = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByPosition($value){
		$sql = 'DELETE FROM categorymaster WHERE Position = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return CategorymasterMySql 
	 */
	protected function readRow($row){
		$categorymaster = new Categorymaster();
		
		$categorymaster->categoryMasterId = $row['CategoryMasterId'];
		$categorymaster->categoryName = $row['CategoryName'];
		$categorymaster->categoryDescription = $row['CategoryDescription'];
		$categorymaster->url = $row['Url'];
		$categorymaster->markForDelete = $row['MarkForDelete'];
		$categorymaster->position = $row['Position'];

		return $categorymaster;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return CategorymasterMySql 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>